package com.sinothk.gpserver.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sinothk.gpserver.modules.comm.base.controller.BaseController;
import com.sinothk.gpserver.modules.system.entity.LoginEntity;
import com.sinothk.gpserver.modules.system.entity.UserEntity;
import com.sinothk.gpserver.modules.system.service.ILoginService;
import com.sinothk.gpserver.modules.system.service.IUserService;
import com.sinothk.gpserver.util.DateUtils;
import com.sinothk.server.support.annotation.callback.ResponseResult;
import com.sinothk.server.support.entity.PageData;
import com.sinothk.server.support.entity.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * a用户相关
 * </p>
 *
 * @author Liang Y.t.
 * @since 2021-08-27
 */
@Api(tags = "用户相关")
@RequestMapping("/user")
@ResponseResult()
@RestController
public class UserController extends BaseController {

    @Autowired
    private IUserService userService;
    @Autowired
    private ILoginService loginService;

    @ApiOperation(value = "分页列表", notes = "分页列表")
    @PostMapping("/page")
    public PageData<UserEntity> page(@RequestBody PageVo<UserEntity> pageVo) {
        return userService.page(pageVo);
    }

    @ApiOperation(value = "详情", notes = "详情")
    @GetMapping("/get/{id}")
    public UserEntity get(@ApiParam("id") @PathVariable String id) {
        return userService.getById(id);
    }

    @ApiOperation(value = "新增", notes = "新增")
    @PostMapping(value = "/add")
    public String add(@RequestBody UserEntity userEntity) {
        LoginEntity login = (LoginEntity) SecurityUtils.getSubject().getPrincipal();
        userEntity.setCreateBy(login.getAccount());
        userEntity.setCreateTime(DateUtils.getLocalDateTime());
        userService.save(userEntity);
        return "新增成功";
    }

    @ApiOperation(value = "删除（逻辑删除）", notes = "删除（逻辑删除）")
    @DeleteMapping("/del/{id}")
    public String del(@ApiParam("id") @PathVariable String id) {
        userService.removeById(id);
        return "已逻辑删除";
    }

    @ApiOperation(value = "修改", notes = "修改")
    @PostMapping("/update")
    public UserEntity update(@RequestBody UserEntity userEntity) {
        userService.updateById(userEntity);

        UserEntity userDb = userService.getById(userEntity.getId());

        // 更新登录数据
        LambdaQueryWrapper<LoginEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LoginEntity::getAccount, userDb.getAccount());

        LoginEntity loginEntity = loginService.getOne(wrapper);
        loginEntity.setPhoneNum(userEntity.getPhoneNum());
        loginEntity.setUserName(userEntity.getUserName());
        loginService.updateById(loginEntity);

        return userService.getById(userEntity.getId());
    }
}
